﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace GameBase.Form.Control
{
    public class MyNumberBox : MyControl
    {
        int min = 0;
        int max = 100;
        int step = 1;

        public int Step
        {
            get { return step; }
            set { step = value; }
        }
        int value;

        
        Sprite dragButton;

        public int Min
        {
            get { return min; }
            set { min = value; }
        }
        public int Max
        {
            get { return max; }
            set { max = value; }
        }
        public int Value
        {
            get { return this.value; }
            set {
                int v = Step*(int)(value / Step);
                if (v< min)
                {
                    this.value = min;
                }
                else if (v > max)
                {
                    this.value = max;
                }
                else
                {
                    this.value = v;
                }
                text = this.value.ToString();
            }
        }
        public override Color Color
        {
            get
            {
                return base.Color;
            }
            set
            {
                base.Color = value;
                dragButton.Color = value;
            }
        }
        public MyNumberBox(Vector2 position)
        {
            dragButton = new Sprite(@"System/dragButton", 3, 1);
            dragButton.SetOriginPosition(ORIGIN_POSITION.MidTop);
            sprite = new Sprite(@"System/numberbox", 1, 1);
            value = 0;
            text = value.ToString();
            Position = position;
            textAlign = Align.Center;
            Margin = 35;
            
        }
        public MyNumberBox(Vector2 position,int min,int max,int step, Color color)
        {
            dragButton = new Sprite(@"System/dragButton", 3, 1);
            dragButton.SetOriginPosition(ORIGIN_POSITION.MidTop);
            sprite = new Sprite(@"System/numberbox", 1, 1);
            
            text = value.ToString();
            Position = position;
            textAlign = Align.Center;
            Margin = 35;
            Min = min;
            Max = max;
            Step = step;
            Value = min;
            Color = color;

        }

        protected override bool IsHover(int x, int y)
        {

            return IsHover(x, y, dragButton.GetBound(GetDragButtonPosition() ,
                screenScale));
        }
        public override void ControlUpdate(GameTime gameTime)
        {
            base.ControlUpdate(gameTime);
            MouseState ms = Mouse.GetState();
            if (ms.LeftButton == ButtonState.Released)
            {
                isPressed = false;
            }
            if (isPressed && ms.LeftButton == ButtonState.Pressed)
            {
                float x = ms.X ;
                float y = ms.Y ;
                float m = Margin * screenScale.X;
                Value = (int)(min + (max - min) * (x - bound.X - m) / (bound.Width - 2 * m));
            }

        }
        bool isPressed = false;
        public override void MousePressed(int x, int y)
        {
            base.MousePressed(x, y);
            isPressed = true;
        }
        /*public override void MouseReleased(int x, int y)
        {
            base.MouseReleased(x, y);
            isPressed = false;
        }*/
        public override void SetFrame(int frame)
        {
            dragButton.CurrentFrame = frame;
        }
        public Vector2 GetDragButtonPosition()
        {
            float m = Margin * screenScale.X;
            return new Vector2(bound.X + (bound.Width - 2*m) * (value - min) / (max-min) + m,
                    bound.Y);
        }
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (!Visible)
            {
                return;
            }
            Sprite.Draw(gameTime, spriteBatch,
                new Vector2(bound.X, bound.Y), screenScale, depth);
            dragButton.Draw(gameTime, spriteBatch, GetDragButtonPosition()
                , screenScale, depth-0.00001f);
            spriteBatch.DrawString(font, text, GetTextPosition(), foreColor,
                0, Vector2.Zero, 1, SpriteEffects.None, depth - 0.00002f);

        }
    }
}
